<template>
  <el-form label-width="100px" ref="formRef" :model="form" :rules="formRules">
    <el-row>
      <el-col :span="12">
        <el-form-item label="父部门" prop="parentId">
          <el-tree-select ref="treeSelect" check-strictly highlight-current node-key="id"
                          :disabled="form.id && (form.code === 'TEN_SUPER_DEPT' || form.code === 'SYS_SUPER_DEPT')"
                          style="width: 100%" :render-after-expand="false" :data="deptList" :props="categoryProps"
                          v-model="form.parentId" @change="parentIdChange"></el-tree-select>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="部门负责人" prop="userId">
          <el-select :disabled="form.id && (form.code === 'TEN_SUPER_DEPT' || form.code === 'SYS_SUPER_DEPT')" style="width: 100%" clearable
                     v-model="form.userId"
                     placeholder="请选择部门负责人">
            <el-option v-for="item in userList" :label="item.name" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="12">
        <el-form-item label="部门标识" prop="code">
          <el-input :disabled="form.id && (form.code === 'TEN_SUPER_DEPT' || form.code === 'SYS_SUPER_DEPT')" clearable placeholder="请输入部门标识"
                    v-model="form.code"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="部门名称" prop="name">
          <el-input clearable placeholder="请输入部门名称" v-model="form.name"></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="12">
        <el-form-item label="联系电话" prop="phone">
          <el-input clearable placeholder="请输入联系电话" v-model="form.phone"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="联系邮箱" prop="email">
          <el-input clearable placeholder="请输入部门邮箱" v-model="form.email"></el-input>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="12">
        <el-form-item label="是否启用" prop="enabled">
          <el-radio-group v-model="form.enabled" :disabled="form.id && (form.code === 'TEN_SUPER_DEPT' || form.code === 'SYS_SUPER_DEPT')">
            <el-radio-button :value="true" border>启用</el-radio-button>
            <el-radio-button :value="false" border>停用</el-radio-button>
          </el-radio-group>
        </el-form-item>
      </el-col>
      <el-col :span="12">
        <el-form-item label="排序" prop="sort">
          <el-input-number
              v-model="form.sort"
              :min="1"
              controls-position="right"
              size="large"
              placeholder="请输入排序"
          ></el-input-number>
        </el-form-item>
      </el-col>
    </el-row>

    <el-row>
      <el-col :span="24">
        <el-form-item label="备注" prop="remark">
          <el-input type="textarea" v-model="form.remark" clearable placeholder="请输入备注"></el-input>
        </el-form-item>
      </el-col>
    </el-row>
  </el-form>
</template>

<script setup lang="js">
import {onMounted, reactive, ref} from "vue";
import tree from "@/utils/tree";
import userApi from "@/api/system/user";
import deptApi from "@/api/system/dept";

const props = defineProps({
  form: {
    type: Object,
    default: {}
  }
})
const formRef = ref();
const deptList = ref([]);
const userList = ref([]);
const categoryProps = {
  label: (data) => data.name + "-" + data.code,
  children: 'children',
}
const formRules = reactive({
  parentId: [{required: true, message: "父部门为必填项", trigger: "blur"}],
  status: [{required: true, message: "状态为必填项", trigger: "blur"}],
  code: [{required: true, message: "部门标识为必填项", trigger: "blur"}],
  name: [{required: true, message: "部门名称为必填项", trigger: "blur"}],
  userId: [{required: true, message: "部门负责人ID为必填项", trigger: "blur"}],
  phone: [{required: true, message: "部门联系电话为必填项", trigger: "blur"}],
  email: [{required: true, message: "部门联系邮箱为必填项", trigger: "blur"}],
  enabled: [{required: true, message: "是否启用为必填项", trigger: "blur"}],
  sort: [{required: true, message: "排序为必填项", trigger: "blur"}],
});
const parentIdChange = (item) => {
  let current = deptList.value.find(dept => dept.id === item);
  if (current) {
    props.form.parentId = current.id;
    props.form.parentName = current.name;
  }
}
const getFormRef = () => {
  return formRef.value;
}

onMounted(() => {
  deptApi.lists.get({enabled: true}).then(response => {
    deptList.value = [...[{
      id: '0',
      code: 'Max',
      name: '顶级部门'
    }], ...tree.buildTree(response.data.list)];
  })
  userApi.lists.get({enabled: true}).then(response => {
    userList.value = response.data.list;
  })
});

defineExpose({getFormRef});
</script>
